/*
   Copyright 2009 Bloo. All Rights Reserved.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package org.youtensils.www.client.widgets;

import java.util.ArrayList;

import org.youtensils.www.client.data.youtube.YouTubeMovie;
import org.youtensils.www.client.events.MovieActionEvent;
import org.youtensils.www.client.events.MovieActionListener;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * A widget that information for a search result entry.
 * @author Bobby Soares
 */
public class SearchResultsEntryPanel extends Composite implements ClickHandler {
	private ArrayList<MovieActionListener> actionListeners;
	private YouTubeMovie resultEntry;
	private HorizontalPanel contentPane;
	private VerticalPanel detailPanel;
	private HorizontalPanel thumbBox, statsPanel, linkPanel;
	private Image thumbnail;
	private Anchor titleLink, youtubeLink, authorLink, playLink, flvLink, mp4Link, mp3Link;
	private StarRating rating;
	/**
	 * A constructor specifying the search result entry and text to highlight.
	 * @param entry the search result entry
	 * @param highlight the text to highlight
	 */
	public SearchResultsEntryPanel(YouTubeMovie entry, String highlight){
		highlight = highlight.toLowerCase();
		this.actionListeners = new ArrayList<MovieActionListener>();
		this.resultEntry = entry;
		this.contentPane = new HorizontalPanel();
		this.thumbBox = new HorizontalPanel();
		this.thumbBox.setStylePrimaryName("gwt-SearchResults-Entry-ThumbBox");
		this.thumbnail = new Image();
		this.thumbnail.setUrl(entry.getThumbnail());
		this.thumbnail.setWidth("120px");
		this.thumbnail.setHeight("72px");
		this.thumbnail.addClickHandler(this);
		this.thumbnail.setStylePrimaryName("gwt-SearchResults-Entry-Thumb");
		this.thumbBox.add(this.thumbnail);
		this.rating = new StarRating(entry.getRating());
		this.authorLink = new Anchor();
		this.authorLink.setText(entry.getAuthor());
		this.authorLink.addClickHandler(this);
		this.statsPanel = new HorizontalPanel();
		this.statsPanel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
		this.statsPanel.setWidth("400px");
		this.statsPanel.add(this.rating);
		this.statsPanel.add(new HTML(entry.getViewCount() + " views"));
		this.statsPanel.add(new HTML(entry.getFavoriteCount() + " times favorited"));
		this.statsPanel.add(authorLink);
		this.linkPanel = new HorizontalPanel();
		this.linkPanel.setStylePrimaryName("gwt-SearchResults-Entry-Menu");
		this.linkPanel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
		this.titleLink = new Anchor();
		this.titleLink.setHTML("<strong>" + entry.getTitle().toLowerCase().replace(highlight, "<b>" + highlight + "</b>") + "</strong>");
		this.titleLink.addClickHandler(this);
		HorizontalPanel title = new HorizontalPanel();
		title.setStylePrimaryName("gwt-SearchResults-Entry-Title");
		title.add(this.titleLink);
		Label detail = new Label(entry.getDurationLabel() + " - " + entry.getDescription(200).replace("\n", ""));
		detail.setStylePrimaryName("gwt-SearchResults-Entry-Description");
		this.detailPanel = new VerticalPanel();
    	this.detailPanel.setHorizontalAlignment(VerticalPanel.ALIGN_LEFT);
    	this.detailPanel.setVerticalAlignment(VerticalPanel.ALIGN_TOP);
    	this.detailPanel.add(title);
    	this.detailPanel.add(detail);
    	this.detailPanel.add(this.statsPanel);
    	this.detailPanel.add(this.linkPanel);
		this.detailPanel.setStylePrimaryName("gwt-SearchResults-Entry-Detail");
		this.youtubeLink = new Anchor();
		this.youtubeLink.setText("Visit YouTube");
		this.youtubeLink.addClickHandler(this);
    	this.playLink = new Anchor();
    	this.playLink.setText("Play");
    	this.playLink.addClickHandler(this);
    	this.flvLink = new Anchor();
    	this.flvLink.setText("Video (FLV)");
    	this.flvLink.addClickHandler(this);
    	this.mp4Link = new Anchor();
    	this.mp4Link.setText("Video (MP4)");
    	this.mp4Link.addClickHandler(this);
    	this.mp3Link = new Anchor();
    	this.mp3Link.setText("Audio (MP3)");
    	this.mp3Link.addClickHandler(this);
    	this.linkPanel.add(youtubeLink);
    	this.linkPanel.add(playLink);
    	this.linkPanel.add(flvLink);
    	//this.linkPanel.add(mp4Link);
    	this.linkPanel.add(mp3Link);
    	this.contentPane.add(this.thumbBox);
    	this.contentPane.add(this.detailPanel);
		initWidget(this.contentPane);
    	setStyleName("gwt-SearchResults-Entry");
	}
	/**
	 * Adds an movie action event listener.
	 * @param listener the event listener to register
	 */
	public void addActionListener(MovieActionListener listener){
		actionListeners.add(listener);
	}
	public void setAlternating(boolean alternate){
	  if(alternate){
      setStyleName("gwt-SearchResults-Entry-Alternate");
	  }
	}
	public void onClick(ClickEvent event) {
		MovieActionEvent evt;
		Object sender = event.getSource();
		if(sender == this.authorLink){
			evt = new MovieActionEvent("author", this.resultEntry);
		}else if(sender == this.playLink || sender == this.thumbnail || sender == this.titleLink){
			evt = new MovieActionEvent("play", this.resultEntry);
		}else if(sender == this.flvLink){
			evt = new MovieActionEvent("flv", this.resultEntry);
		}else if(sender == this.mp4Link){
			evt = new MovieActionEvent("mp4", this.resultEntry);
		}else if(sender == this.mp3Link){
			evt = new MovieActionEvent("mp3", this.resultEntry);
		}else if(sender == this.youtubeLink){
			Window.open(this.resultEntry.getLocation(), "YouTube", "");
			return;
		}else return;
		for(int i=0; i<this.actionListeners.size(); i++){
			this.actionListeners.get(i).onAction(evt, this);
		}
	}
}
